package com.cr.crandroid.ui.person.publish;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

import com.cr.crandroid.R;
import com.cr.crandroid.adapter.TabFragmentAdapter;
import com.cr.crandroid.base.BaseFragment2;
import com.cr.crandroid.ui.home.HomeFragment;
import com.cr.crandroid.ui.person.PersonFragment;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;


public class PublishFragment extends Fragment {
    private String[] tabNames = new String[]{"未发布", "已发布"};
    private List<Fragment> fragments = new ArrayList<>();
    private SegmentTabLayout tabLayout;
    private TabFragmentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_publish, container, false);
        tabLayout = root.findViewById(R.id.tabs);
        ViewPager viewPager = root.findViewById(R.id.viewPager);

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);

            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        fragments.add(new UnpublishedFragment());
        fragments.add(new PublishedFragment());

        adapter = new TabFragmentAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Fragment item = adapter.getItem(position);
                item.onResume();
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setTabData(tabNames);

        return root;
    }
}